
<link rel="import" href="../polymer/polymer.html">

<!--
Enables a custom element to be included as an `mqtt-subscription` in an `mqtt-connection`.
-->
<script>
  (function(scope) {
    var MqttElements = scope.MqttElements = scope.MqttElements || {};

    /**
     @demo demo/index.html
     @polymerBehavior
     */
    MqttElements.MqttSubscriptionRegestrationBehaviour = {

      properties: {
        /**
         * Fired when the element is added to an `mqtt-connection`.
         *
         * @event mqtt-subscription-register
         */

        /**
         * Fired when the element is removed from an `mqtt-connection`.
         *
         * @event 'mqtt-subscription-unregister
         */

        /**
         * The `mqtt-connection` that the element is registered to.
         */
        _parentConnection: {
          type: Object,
        },
      },

      attached: function() {
        // Note: the `mqtt-connection` that this element belongs to will set this
        // element's _parentConnection property when handling this event.
        this.fire('mqtt-subscription-register', {topic: this.topic, qos: this.qos});
      },

      detached: function() {
        if (this._parentConnection) {
          this._parentConnection.fire('mqtt-subscription-unregister', {target: this});
        }
      },
    }

  })(window)
</script>